﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblBookingDAO
    {
        private DBService _db = null;
        public TblBookingDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public bool Insert(TblBooking booking, List<TblBookingDetail> lstDetail, out int BookID)
        {
            BookID = 0;
            bool flag = false;
            try
            {

                DataTable dt = _db.ExeSP_DataTable("sp_TblBookingIns",
                   new string[] { "@DateBook", "@DateEvent", "@TimeEvent", "@Meal", "@NumberPeople", "@Status", "@Node", "@IDType", "@CusID", "@TotalPrice" },
                   new object[] { 
                                       Convert.ToDateTime(booking.DateBook),
                                        Convert.ToDateTime(booking.DateEvent),
                                        Code.FixStringNull(booking.TimeEvent), 
                                        Code.FixStringNull(booking.Meal),
                                        Code.FixIntNull(booking.NumberPeople).ToString(),
                                        Code.FixIntNull(booking.Status).ToString(),
                                        Code.FixStringNull(booking.Notes),
                                        Code.FixIntNull(booking.IDType).ToString(),
                                        Code.FixIntNull(booking.CusID).ToString(),
                                        Convert.ToDecimal(booking.TotalPrice),
                 
                    }, CommandType.StoredProcedure);

                foreach (TblBookingDetail bookingDetail in lstDetail)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        bookingDetail.TotalAmount = bookingDetail.Quantity * bookingDetail.Price;

                        _db.ExeSP_DataTable("sp_TblBookingDetail_Ins",
                          new string[] { "@BookID", "@IDMenu", "@Quantity", "@Price", "@TotalAmount", "@Discount" },
                          new object[] { dr["BookID"],    
                                    Code.FixIntNull(bookingDetail.IDMenu).ToString(),
                                    Code.FixIntNull(bookingDetail.Quantity).ToString(),
                                    Convert.ToDecimal(bookingDetail.Price), 
                                    Convert.ToDecimal(bookingDetail.TotalAmount),
                                    bookingDetail.Discount},
                                         CommandType.StoredProcedure);

                        BookID = Convert.ToInt32(dr["BookID"].ToString());
                    }

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable Insert_UpBooking(TblBooking booking)
        {
            DataTable dt = null;

            try
            {

                dt = _db.ExeSP_DataTable("sp_TblBookingIns",

                    new String[] {  "@DateBook", "@DateEvent", "@TimeEvent", "@Meal", "@NumberPeople", "@Status", 
                                    "@Node", "@IDType", "@CusID", "@TotalPrice" },

                    new object[] { booking.DateBook, booking.DateEvent, booking.TimeEvent, booking.Meal, booking.NumberPeople, booking.Status,
                                    booking.Notes, booking.IDType, booking.CusID, booking.TotalPrice},
                        CommandType.StoredProcedure);


            }
            catch (Exception e)
            {
                throw e;
            }
            return dt;
        }

        public DataTable Insert_UpBookingDetail(TblBookingDetail bookingDetail)
        {
            DataTable dt = null;

            try
            {

                dt = _db.ExeSP_DataTable("sp_TblBookingDetail_Ins",
                    new String[] { "@BookID", "@IDMenu", "@Quantity", "@Price", "@TotalAmount", "@Discount"},
                    new object[] {bookingDetail.BookID, bookingDetail.IDMenu, bookingDetail.Quantity, bookingDetail.Price, bookingDetail.TotalAmount, bookingDetail.Discount}, CommandType.StoredProcedure);


            }
            catch (Exception e)
            {
                throw e;
            }
            return dt;
        }

        public bool CheckDatetime(string date, string time)
        {
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_CheckDatetime",
                    new string[] { "@DateEvent", "@TimeEvent" },
                    new object[] { date, time },
                    CommandType.StoredProcedure);
                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetOrderLst(string p_DateEvent, int p_IDType, string p_Status)
        {
            try
            {
                return _db.ExeSP_DataTable("sp_Order_List",
                    new string[] { "@p_DateEvent", "@p_TypeParty", "@p_Status" },
                    new object[] { p_DateEvent, p_IDType, "", p_Status }, CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public DataTable Update(int Status, int BookID)
        {
            DataTable dt = null;
            try
            {

                dt = _db.ExeSP_DataTable("Update TblBooking set Status= @Status where BookID = @BookID",
                    new string[] { "@Status", "@BookID" },
                    new object[] { Status, BookID }, CommandType.Text);


            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetOrder(int id)
        {
            try
            {
                return _db.ExeSP_DataTable("sp_GetOrder",
                    new string[] { "@BookID" }
                    , new object[] { id }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
